package com.lepu.app.fun.chat.core;


import com.app.utils.Setting;

import org.jivesoftware.smack.Roster;
import org.jivesoftware.smack.RosterEntry;
import org.jivesoftware.smack.SmackException;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smackx.vcardtemp.packet.VCard;


/**
 * Created by Administrator on 2015/10/23.
 */
public class XmppUserManager {


    static class XmppUserManagerHolder {
        static XmppUserManager xmppUserManager = new XmppUserManager();
    }

    public static XmppUserManager getInstance() {
        return XmppUserManagerHolder.xmppUserManager;
    }

    /**
     * 得到 链接
     *
     * @return
     */
    public XMPPConnection getXmppConnext() {
        XMPPConnection connection = ClientChatXmppImpl.getInstance().getConnection();
        if (connection.isConnected()) {
            return ClientChatXmppImpl.getInstance().getConnection();
        }
        return null;
    }

    /**
     * 获取用户的vcard信息
     *
     * @param user
     * @return
     * @throws XMPPException
     */
    public VCard getUserVCard(String user) throws XMPPException {
        VCard vcard = new VCard();
        try {
            vcard.load(getXmppConnext(), user);
        } catch (SmackException.NoResponseException e) {
            e.printStackTrace();
        } catch (SmackException.NotConnectedException e) {
            e.printStackTrace();
        }

        return vcard;
    }

    /**
     * 获取用户头像信息
     */
    public VCard getUserVcard(String user) {
        try {
            VCard vcard = new VCard();
            vcard.load(getXmppConnext(), user);
            if (vcard != null) {
                return vcard;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 移除指定的好友
     *
     * @param friendJid
     */
    public void removeFriend(String friendJid) {
        try {
            XMPPConnection connection = getXmppConnext();
            Roster roster = connection.getRoster();
            RosterEntry entry = roster.getEntry(friendJid + "@" + Setting.CHAT_SERVICE_HOST);
            if (entry != null)
                roster.removeEntry(entry);
        } catch (SmackException.NotLoggedInException e) {
            e.printStackTrace();
        } catch (SmackException.NoResponseException e) {
            e.printStackTrace();
        } catch (XMPPException.XMPPErrorException e) {
            e.printStackTrace();
        } catch (SmackException.NotConnectedException e) {
            e.printStackTrace();
        }
    }
}
